package cn.afactory.tygz.ui.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.afactory.tygz.R;
import cn.afactory.tygz.db.DBManager;
import cn.afactory.tygz.model.UserInfo;
import cn.afactory.tygz.ui.base.BaseActivity;
import cn.afactory.tygz.ui.base.BaseFragmentActivity;
import cn.afactory.tygz.ui.fragments.MoneyDetailFragment;
import cn.afactory.tygz.ui.fragments.MoneyRecordFragment;
import cn.afactory.tygz.ui.fragments.MoneyShopFragment;
import cn.afactory.tygz.ui.fragments.ScenicSpotsListFragment;
import cn.afactory.tygz.ui.sns.fragment.TopicListFragment;

/**
 * Created by xi on 15/10/11.
 */
public class MyMoneyActivity extends BaseFragmentActivity implements View.OnClickListener {
    private LinearLayout ll_shop,ll_detail,ll_record;
    private TextView tv_shop,tv_detail,tv_record;
    private ImageView iv_shop,iv_detail,iv_record;
    private ViewPager mViewPager;

    public static final int SHOP = 0;
    public static final int DETAIL = 1;
    public static final int RECORD = 2;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        setContentView(R.layout.ac_my_money);

        initViews();
//        setRightButton(mContext.getString(R.string.label_btn_money_in), new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

        UserInfo userInfo = DBManager.getInstance(getApplicationContext()).getDefaultUserInfo();
        if(userInfo != null){
            setTitleText("我的金币("+userInfo.money+")");
        }else{

            finish();

        }

    }

    private void initViews(){
        ll_shop = (LinearLayout) findViewById(R.id.ll_shop);
        ll_shop.setOnClickListener(this);
        ll_detail = (LinearLayout) findViewById(R.id.ll_detail);
        ll_detail.setOnClickListener(this);
        ll_record= (LinearLayout) findViewById(R.id.ll_record);
        ll_record.setOnClickListener(this);


        mViewPager = (ViewPager) findViewById(R.id.vp_task_list);
        tv_shop = (TextView) findViewById(R.id.tv_shop);
        tv_detail = (TextView) findViewById(R.id.tv_detail);
        tv_record = (TextView) findViewById(R.id.tv_record);

        iv_shop = (ImageView) findViewById(R.id.iv_shop);
        iv_detail = (ImageView) findViewById(R.id.iv_detail);
        iv_record = (ImageView) findViewById(R.id.iv_record);


        AttentionListViewPagerAdapter adapter = new AttentionListViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int idx) {
                if (idx == SHOP) {
                    switchTab(SHOP);
                } else if (idx == DETAIL) {
                    switchTab(DETAIL);
                }else if (idx == RECORD) {
                    switchTab(RECORD);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        switchTab(SHOP);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_shop:
                switchTab(SHOP);
                break;
            case R.id.ll_detail:
                switchTab(DETAIL);
                break;
            case R.id.ll_record:
                switchTab(RECORD);
                break;
        }
    }

    class AttentionListViewPagerAdapter extends FragmentPagerAdapter {
        public AttentionListViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int idx) {
            Fragment fragment = null;
            switch (idx){
                case SHOP:
                    fragment = new MoneyShopFragment();
                    break;
                case DETAIL:
                    fragment = new MoneyDetailFragment();
                    break;
                case RECORD:
                    fragment = new MoneyRecordFragment();
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }
    }
    //tab之间切换
    private void switchTab(int index) {
        clearTabStatus();
        mViewPager.setCurrentItem(index, true);
        switch (index){
            case SHOP:
                ll_shop.setSelected(true);
                tv_shop.setSelected(true);
                iv_shop.setVisibility(View.VISIBLE);
                break;
            case DETAIL:
                ll_detail.setSelected(true);
                tv_detail.setSelected(true);
                iv_detail.setVisibility(View.VISIBLE);
                break;
            case RECORD:
                ll_record.setSelected(true);
                tv_record.setSelected(true);
                iv_record.setVisibility(View.VISIBLE);
                break;
        }
    }
    //清除Tab选择状态
    private void clearTabStatus(){
        ll_shop.setSelected(false);
        tv_shop.setSelected(false);
        iv_shop.setVisibility(View.GONE);

        ll_detail.setSelected(false);
        tv_detail.setSelected(false);
        iv_detail.setVisibility(View.GONE);

        ll_record.setSelected(false);
        tv_record.setSelected(false);
        iv_record.setVisibility(View.GONE);
    }
}
